package de.gameduell.framework.gui.scroll {
	import flash.display.Sprite;
	
	import de.gameduell.framework.debug.Debug;
	
	import fl.containers.ScrollPane;	

	/**
	 * @author cbra
	 */
	public class ScrollList {

		private var container:Sprite;
		private var _scrollPane:ScrollPane;
		private var itemList:Array;
		private var _autoPlace:Boolean;

		public function ScrollList(aScrollPane:ScrollPane, aAutoPlace:Boolean = false) {			
			_autoPlace = aAutoPlace;
			container  = new Sprite();
			itemList = new Array();
			_scrollPane = aScrollPane;
			aScrollPane.source = container;
		}

		public function addItem(aSprite:Sprite):void {
			container.addChild(aSprite);
//			_scrollPane.refreshPane();
			if(_autoPlace){
				itemList.push(aSprite);
			}
			update();
		}

		public function update():void {
			if(_autoPlace){
				var currYpos:Number = 0;
				for each(var item:Sprite in itemList){
					item.y = currYpos;
					currYpos += item.height;
				}
			}
			_scrollPane.refreshPane();
		}
		
		public function get scrollPane():ScrollPane {
			return _scrollPane;
		}

		public function scrollToBottom():void {
			try {
				_scrollPane.verticalScrollPosition = container.height;
			} catch (e:Error) {
				Debug.trace("ScrollList::scrollToBottom e " + e, Debug.WARNING);
			}
		}
	}
}
